package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void saveShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId( BaseContext.getCurrentId());
        //1.判断当前购物车商品，是否已经存在
        List<ShoppingCart> list =  shoppingCartMapper.list(shoppingCart);
if (list != null && list.size()>0){
    //2.数据存在数量+1
    ShoppingCart cart = list.get(0);
    cart.setNumber(cart.getNumber() + 1);
    shoppingCartMapper.updateNumberById(cart);
}else {
    //3.如果不存在则需要插入购物车
    // 判断是菜品还是套餐
    Long dishId = shoppingCartDTO.getDishId();
    shoppingCart.setNumber(1);
    shoppingCart.setCreateTime(LocalDateTime.now());
    if (dishId != null){
        //本次添加到购物车的是菜品
        //查询菜品表得出数据
        Dish dish = dishMapper.getById(dishId);
        shoppingCart.setName(dish.getName());
        shoppingCart.setImage(dish.getImage());
        shoppingCart.setAmount(dish.getPrice());


    }else {
        //本次添加的是套餐
        //查询套餐表得出数据
        Setmeal setmeal = setmealMapper.selectSeatmeal(shoppingCartDTO.getSetmealId());
//shoppingCart.setNumber(1);
shoppingCart.setAmount(setmeal.getPrice());
shoppingCart.setName(setmeal.getName());
shoppingCart.setImage(setmeal.getImage());
//shoppingCart.setCreateTime(LocalDateTime.now());

    }
//将封装好的数据插入数据库
shoppingCartMapper.insert(shoppingCart);

}
    }
/*查询购物车*/
    @Override
    public List<ShoppingCart> selectshoppingCart() {

        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }

    /**
     * 删除购物车中的一个菜品
     * @param shoppingCartDTO
     */
    @Override
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
       shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size()>0 ){
            ShoppingCart sc = list.get(0);
            //1.如果购物车某菜品只有一份则直接删除
            if (sc.getNumber() == 1) {
                shoppingCartMapper.delete(shoppingCartDTO);
            } else {
                //2.如果购物车菜品有多份则修改数量
                shoppingCart.setNumber(sc.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);

            }

        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAll(userId);
    }


}
